package ihm;

import java.awt.CardLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import xml_parser.ParserXmlFile;
import database.DataBase;

public class FragmentPanel extends javax.swing.JPanel{
	
	private MainFrame frame;
	private JFileChooser fc; 
	DataBase db;
	private String choosedFile;
	private String processName, fragmentName, positionFragment;
	
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton4;
	private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;

	

	public FragmentPanel(MainFrame f) {
		super(new FlowLayout());
		frame=f;
		db=new DataBase(frame.getDbFile());
		 initComponents(db.getProcessNameList(),db.getAllFragmentNameList());
	}
	 private void initComponents(HashSet<String> p,HashSet<String> f) {

	       
	        jComboBox1 = new javax.swing.JComboBox(p.toArray());
	        jLabel1 = new javax.swing.JLabel();
	        jLabel2 = new javax.swing.JLabel();
	        jComboBox2 = new javax.swing.JComboBox(f.toArray());
	        jLabel3 = new javax.swing.JLabel();
	        jTextField1 = new javax.swing.JTextField();
	        jLabel4 = new javax.swing.JLabel();
	        jButton1 = new javax.swing.JButton();
	        jTextField2 = new javax.swing.JTextField();
	        jButton2 = new javax.swing.JButton();
	        jButton4 = new javax.swing.JButton();
	        jButton5 = new javax.swing.JButton();


	        jComboBox1.setEditable(true);
	        jComboBox1.setSelectedItem(" ");
	        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jComboBox1ActionPerformed(evt);
	            }
	        });

	        jLabel1.setText("Process Name :");

	        jLabel2.setText("Fragment Name :");

	        jComboBox2.setEditable(true);
	        jComboBox2.setSelectedItem(" ");
	        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jComboBox2ActionPerformed(evt);
	            }
	        });

	        jLabel3.setText("Fragment Position :");

	        jTextField1.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jTextField1ActionPerformed(evt);
	            }
	        });

	        jLabel4.setText("Diagram URL : ");

	        jButton1.setText("Browse");
	        jButton1.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jButton1ActionPerformed(evt);
	            }
	        });

	        jButton2.setIcon(new javax.swing.ImageIcon("C:\\Users\\Noelie\\Pictures\\valider2.png")); // NOI18N
	        jButton2.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                try {
						jButton2ActionPerformed(evt);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }
	        });
	        
	        jButton4.setIcon(new javax.swing.ImageIcon("C:\\Users\\Noelie\\Pictures\\styloOk.png")); 
	        jButton4.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jButton4ActionPerformed(evt);
	            }
	        });
	        jButton5.setIcon(new javax.swing.ImageIcon("C:\\Users\\Noelie\\Pictures\\suppr.gif")); 
	        jButton5.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jButton5ActionPerformed(evt);
	            }
	        });

	        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(this);
	        this.setLayout(jPanel1Layout);
	        jPanel1Layout.setHorizontalGroup(
	            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
	                	.addGroup(jPanel1Layout.createSequentialGroup()
	                        .addGap(300, 300, 300)
	                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addGap(18, 18, 18)
	                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addGap(18, 18, 18)
	                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
	                    .addGroup(jPanel1Layout.createSequentialGroup()
	                        .addGap(20, 20, 20)
	                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addGroup(jPanel1Layout.createSequentialGroup()
	                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
	                                    .addComponent(jLabel3))
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
	                            .addGroup(jPanel1Layout.createSequentialGroup()
	                                .addComponent(jLabel4)
	                                .addGap(111, 111, 111)))
	                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
	                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                                .addComponent(jButton1))
	                            .addComponent(jComboBox2, 0, 228, Short.MAX_VALUE)
	                            .addComponent(jComboBox1, 0, 228, Short.MAX_VALUE)
	                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE))))
	                .addContainerGap())
	        );
	        jPanel1Layout.setVerticalGroup(
	            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanel1Layout.createSequentialGroup()
	                .addGap(58, 58, 58)
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addGap(18, 18, 18)
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addGap(18, 18, 18)
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addGap(18, 18, 18)
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                        .addComponent(jButton1)
	                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
	                .addGap(48, 48, 48)
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
	                		.addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                		.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                		.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	                .addContainerGap(30, Short.MAX_VALUE))
	        );
    
	    }

	   
	    private void jButton5ActionPerformed(ActionEvent evt) {
	    	if(fragmentName==null || (!db.containsFragment(fragmentName)|| processName==null || (!db.containsProcess(processName))) ){
	    		JOptionPane.showMessageDialog(null, "Choose a process and a fragment name in the list",
	                    "Warning", JOptionPane.WARNING_MESSAGE);
	    	}
	    	else{
				db.removeFragment(processName,fragmentName);
				jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(db.getProcessNameList().toArray()));
			    jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(db.getAllFragmentNameList().toArray()));
			    jComboBox1.setSelectedIndex(-1);
			    jComboBox2.setSelectedIndex(-1);
			    jTextField1.setText("");
			    jTextField2.setText("");
				JOptionPane.showMessageDialog(null, "Fragment remove",
	                    "Information", JOptionPane.INFORMATION_MESSAGE);
	    	}
		
	}
		private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
	       positionFragment=jTextField1.getText();
	    }                                           

	    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
	        // TODO add your handling code here:
	    	fc = new JFileChooser(jTextField2.getText());
			fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
			int returnVal = fc.showOpenDialog(FragmentPanel.this);
			if (returnVal == JFileChooser.APPROVE_OPTION) 
			{
				choosedFile = fc.getSelectedFile().getPath();
				jTextField2.setText(choosedFile);
			} 
			
	    }                                        

	    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
			processName=(String) jComboBox1.getSelectedItem();
			ArrayList<String> comp=new ArrayList<String>();
    		comp.addAll(db.getFragmentNameList(processName));
    		jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(comp.toArray()));
    		jComboBox2.setSelectedIndex(-1);
	    }      
	    
	    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
	    	fragmentName= (String) jComboBox2.getSelectedItem();
	    }                                          


	    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
	    	positionFragment=jTextField1.getText();
	    	//System.out.println(fragmentName+" "+processName+" "+positionFragment+" "+choosedFile+" ");
	    	if(fragmentName==null || processName==null || positionFragment==null || choosedFile==null ){
	    		JOptionPane.showMessageDialog(null, "Complete all field",
	                    "Warning", JOptionPane.WARNING_MESSAGE);
	    	}
	    	else{
	    		OutputStream out=new FileOutputStream("src/file.txt");
				String in=this.choosedFile;
				
				ParserXmlFile xp = new ParserXmlFile();
				System.out.println("start parse file");
				xp.runParser(in,out);
				System.out.println("parse done");
				
				
				String file="fragment.xml";
				positionFragment=jTextField1.getText();
				if(db.addComponentInDatabase(this.processName, this.fragmentName, this.positionFragment, xp.getComponent())){
					JPanel oper=new OperationPanel(db.getFragmentList(processName),processName,frame);
					frame.getPan().add(oper, processName);
					frame.setTitle(processName);
					CardLayout cl = (CardLayout)(frame.getPan().getLayout());
					cl.show(frame.getPan(), processName);
					//System.out.println(db.toString());
				}
				else{
					JOptionPane.showMessageDialog(null, "fragment already existing or wrong number",
		                    "Fill in error", JOptionPane.WARNING_MESSAGE);
				}
	    	}
	    }

	    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
	    	if(processName==null || (!db.containsProcess(processName)) ){
	    		JOptionPane.showMessageDialog(null, "Choose a process name",
	                    "Warning", JOptionPane.WARNING_MESSAGE);
	    	}
	    	else{
				Component[] panels= frame.getPan().getComponents();
				//System.out.println(panels.toString());
				CardLayout cl = (CardLayout)(frame.getPan().getLayout());
				JPanel oper=new OperationPanel(db.getFragmentList(processName),processName,frame);
				frame.getPan().add(oper,processName);
				frame.setTitle(processName);
				
				cl.show(frame.getPan(), processName);
				//System.out.println("edit process: " + processName);
	    	}
	    }
	    

	    public String getSelectedFile()
		{
			return choosedFile;
		}
	    
	    public String getProcessName() {
			return processName;
		}
	    
	    public String getPositionFargment(){
	    	return positionFragment;
	    }


		public String getFragmentName() {
			return fragmentName;
		}

	
	
}
